package com.cold.legendary.bprocess.test.generator;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.util.ArrayList;
import java.util.List;

/**
 * @author cold
 * @since 2023/3/6
 */
public class Generator {

    public static void main(String[] args) throws Exception {
        String lDbPath = "E:\\code\\legendary";
        String lDbUrl = "jdbc:mysql://MYSQL_URL:3306/MYSQL_DB?useUnicode=true&amp;characterEncoding=UTF-8";
        String lDbUsername = "MYSQL_NAME";
        String lDbPassword = "MYSQL_PWD";

        //设置自己工程所在目录，在XML文件中会做替换
        System.setProperty("lDbPath", lDbPath);
        System.setProperty("lDbUrl", lDbUrl);
        System.setProperty("lDbUsername", lDbUsername);
        System.setProperty("lDbPassword", lDbPassword);

        List<String> warnings = new ArrayList<>();
        boolean overwrite = true;
        Resource resource = new ClassPathResource("/mybatisGeneratorConfig.xml");
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(resource.getFile());
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);
    }
}
